package com.itheima.health.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.health.entity.TOrdersetting;
import com.itheima.health.mapper.TOrdersettingMapper;
import com.itheima.health.pojo.dto.TOrdersettingVO;
import com.itheima.health.pojo.vo.Region;
import com.itheima.health.service.ITOrdersettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.utils.SorageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cpanda
 * @since 2024-01-29
 */
@Service
public class TOrdersettingServiceImpl extends ServiceImpl<TOrdersettingMapper, TOrdersetting> implements ITOrdersettingService {
    @Autowired
    ITOrdersettingService ordersettingService;

    /**
     * 查看预约设置
     *
     * @param month
     * @return
     */
    @Override
    public List<TOrdersettingVO> getOrderSettingByMonth(String month) {
        String outputDate = convertDateFormat(month);
        System.out.println(outputDate); // 输出转换后的日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(outputDate, formatter);
        System.out.println("Date: " + startDate);
        // 获取该月份的最后一天
        LocalDate endDate = startDate.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("lastDayOfMonth: " + endDate);

        LambdaQueryWrapper<TOrdersetting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(TOrdersetting::getOrderDate, startDate)
                .lt(TOrdersetting::getOrderDate, endDate);
        List<TOrdersetting> ordersettings = this.list(queryWrapper);//根据时间查询出来所有 符合的对象
        ArrayList<TOrdersettingVO> voList = new ArrayList<>();

        for (TOrdersetting ordersetting : ordersettings) {
            LocalDate orderDate = ordersetting.getOrderDate();
            int day = orderDate.getDayOfMonth();//取天数
            TOrdersettingVO vo = new TOrdersettingVO();
            vo.setDate(day);
            vo.setNumber(ordersetting.getNumber());
            vo.setReservations(ordersetting.getReservations());
            voList.add(vo);
        }
        return voList;
    }

    public static String convertDateFormat(String month) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-M");
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = inputFormat.parse(month);
            return outputFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 读出问价加载到数据库
     *
     * @return
     */

    @Override
    public void upload(MultipartFile excelFile) {
        InputStream is = null;
        try {
            is = excelFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //1.进行读取数据，StorageRetrieval是我的pojo类，
        //2.new SorageListener(storageService)这个是监听器，主要用来读取数据的，别急后面会讲
        //3.特别注意的是storageService这个service，我上面有注入进去 @Autowired，切记不要new会报错
        EasyExcel.read(is, Region.class, new SorageListener(ordersettingService)).sheet().doRead();
    }
}
